﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MvvmEx.Controls.Messager
{
    public class MessageBoxBase : Control
    {
        #region 字段

        private Border _border;

        #endregion

        #region 属性

        public Controls.WrapperView Window
        {
            get; set;
        }

        #endregion

        #region 依赖属性

        #region 标题
        public new string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(MessageBoxBase), new PropertyMetadata(string.Empty));
        #endregion

        #region 内容
        public new string Content
        {
            get
            {
                return (string)GetValue(ContentProperty);
            }
            set
            {
                SetValue(ContentProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(string), typeof(MessageBoxBase), new PropertyMetadata(string.Empty));
        #endregion

        #endregion

        #region 命令
        public static readonly RoutedCommand CloseCommand = new RoutedCommand("Close", typeof(MessageBox));
        #endregion


        #region 静态构造
        static MessageBoxBase()
        {
            InitCommands();
        }
        #endregion

        #region 构造

        public MessageBoxBase()
        {
            Window = new Controls.WrapperView();
        }

        #endregion

        #region 公有方法

        #region 关闭窗口

        public virtual void Close()
        {
            Window?.Close();
        }

        #endregion

        #region 显示窗口

        public void ShowDialog()
        {
            Window.Border.Child = this;
            Window?.ShowDialog();
        }

        #endregion

        #endregion

        #region 私有方法

        #region 注册命令
        private static void InitCommands()
        {
            var excutedRoutedEventHandler = new ExecutedRoutedEventHandler(OnMessageBoxCommand);

            CommandManager.RegisterClassCommandBinding(typeof(MessageBoxBase), new CommandBinding(MessageBoxBase.CloseCommand, excutedRoutedEventHandler));
        }
        #endregion

        #region 命令实现
        private static void OnMessageBoxCommand(object target, ExecutedRoutedEventArgs e)
        {
            if (e.Command == MessageBox.CloseCommand)
            {
                ((MessageBoxBase)target).Close();
            }
        }
        #endregion

        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _border = this.GetTemplateChild("Border") as Border;

            if (_border != null)
            {
                _border.MouseLeftButtonDown -= _border_MouseLeftButtonDown;
                _border.MouseLeftButtonDown += _border_MouseLeftButtonDown;
            }


        }

        private void _border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
            {
                Window.DragMove();
            }
        }

    }
}